home *** CD-ROM | disk | FTP | other *** search
- /* Moniker 2.0.1 */
- /* Copyright ©1992 by Michael J. Simms */
- /* Modified 12/20/92 by Sean Hummel */
-
-
- /* Include the program header. */
- #include "Moniker 2.0.1.h"
- #include "AppleEvents.h"
- #include "Aliases.h"
- #include "GestaltEqu.h"
- #include "Traps.h"
- #include "Memory.h"
- #include "string.h"
-
- /* Function prototypes. */
- OSErr makeAlias(FSSpec);
- void initialize(void);
- Boolean canRunProgram(void);
- void installAEHandlers(void);
- void removeAEHandlers(void);
- void showExplainBox(void);
- short NumToolboxTraps(void);
- TrapType GetTrapType(short);
- Boolean TrapAvailable(short);
- void describeError(short);
-
- /* Declarations. */
- extern pascal OSErr HandleODOC(AppleEvent *, AppleEvent *, long);
- extern pascal OSErr HandleQUIT(AppleEvent *, AppleEvent *, long);
- extern pascal OSErr HandleOAPP(AppleEvent *, AppleEvent *, long);
- extern pascal OSErr HandlePDOC(AppleEvent *, AppleEvent *, long);
- extern Boolean gQuitDemand;
-
-
- OSErr
- makeAlias(FSSpec fileFSSpec)
- /* Create an alias. */
- {
- short aliasRefNum,myResFile;
- OSErr myErr;
- Point where={84,50};
- FInfo aliasFInfo,theFileFInfo;
- Str255 saveString;
- FSSpec aliasFSSpec;
- AliasHandle aliasHandle;
- StandardFileReply reply;
- EventRecord event;
- Str255 volName;
-
- /* Call Standard File Package. */
- GetIndString(saveString,kSaveSTRs,kSaveStr);
-
- if (!gNameManually) {
- CustomPutFile(saveString,fileFSSpec.name,&reply,kPutFile,where,NIL,NIL,NIL,NIL,NIL);
- }/*if*/
- else { HGetVol(volName,&reply.sfFile.vRefNum,&reply.sfFile.parID);
- reply.sfGood = TRUE;
- BlockMove(&fileFSSpec.name,&reply.sfFile.name,sizeof(reply.sfFile.name));
- }/*else*/
-
-
-
-
- /* If the user gave the go-ahead then save the file. */
- if (reply.sfGood) {
-
- /* Fill the 'aliasFSSpec' structure. */
- FSMakeFSSpec(reply.sfFile.vRefNum,reply.sfFile.parID,
- reply.sfFile.name,&aliasFSSpec);
-
- /* Create the alias record. */
- myErr=NewAlias(NIL,&fileFSSpec,&aliasHandle);
-
- /* Save the current resource file. */
- myResFile=CurResFile();
-
- /* Get info on the file we are making an alias of. */
- FSpGetFInfo(&fileFSSpec,&theFileFInfo);
-
- /* Create and open the file. */
- FSpCreateResFile(&aliasFSSpec,theFileFInfo.fdCreator,theFileFInfo.fdType,reply.sfScript);
- aliasRefNum=FSpOpenResFile(&aliasFSSpec,fsCurPerm);
-
- /* If possible, write to the file. */
- if (aliasRefNum!=kCantOpen) {
-
- /* Write the resource. */
- UseResFile(aliasRefNum);
- AddResource((Handle)aliasHandle,rAliasType,kAliasRsrcID,aliasFSSpec.name);
- WriteResource((Handle)aliasHandle);
- CloseResFile(aliasRefNum);
-
- /* Set the finder flags. */
- theFileFInfo.fdFlags=kAliasFlag;
- FSpSetFInfo(&aliasFSSpec,&theFileFInfo);
-
- } else describeError(kCantOpen);
-
- /* Set resource file back to this application's. */
- UseResFile(myResFile);
- }
- }
-
-
- void
- initialize(void)
- /* Initialization routine. */
- {
- MaxApplZone();
- FlushEvents(everyEvent,0);
-
- /* Initialize stuff. */
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NIL);
- InitCursor();
-
- /* Draw the menu bar. */
- DrawMenuBar();
- }
-
-
- Boolean
- canRunProgram(void)
- /* Returns TRUE if the program can run in the current environment. */
- {
- long myErr,response;
- Boolean haveGestalt,haveAliasMgr=FALSE,haveAppleEvents=FALSE,canRun;
-
- /* Get information on the current system setup. */
- if (haveGestalt=TrapAvailable(_GestaltDispatch)) {
-
- if (Gestalt(gestaltAliasMgrAttr,&response)==noErr) {
- /* The default for 'haveAliasMgr' is FALSE. */
- if (BitTst(&response,31-gestaltAliasMgrPresent)) haveAliasMgr=TRUE;
- }
-
- if (Gestalt(gestaltAppleEventsAttr,&response)==noErr) {
- /* The default for 'haveAppleEvents' is FALSE. */
- if (BitTst(&response,31-gestaltAppleEventsPresent)) haveAppleEvents=TRUE;
- }
-
- canRun=haveAliasMgr&&haveAppleEvents;
- }
-
- /* Return TRUE if we can run this program. */
- return(canRun);
- }
-
-
- void
- installAEHandlers(void)
- /* Installs Apple Event handlers. */
- {
- AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
- (EventHandlerProcPtr)HandleODOC,0,FALSE);
- AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
- (EventHandlerProcPtr)HandleQUIT,0,FALSE);
- AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
- (EventHandlerProcPtr)HandlePDOC,0,FALSE);
- AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
- (EventHandlerProcPtr)HandleOAPP,0,FALSE);
- }
-
-
- void
- removeAEHandlers(void)
- /* Removes Apple Event Handlers. */
- {
- AERemoveEventHandler(kCoreEventClass,kAEOpenDocuments,
- (EventHandlerProcPtr)HandleODOC,FALSE);
- AERemoveEventHandler(kCoreEventClass,kAEQuitApplication,
- (EventHandlerProcPtr)HandleQUIT,FALSE);
- AERemoveEventHandler(kCoreEventClass,kAEPrintDocuments,
- (EventHandlerProcPtr)HandlePDOC,FALSE);
- AERemoveEventHandler(kCoreEventClass,kAEOpenApplication,
- (EventHandlerProcPtr)HandleOAPP,FALSE);
- }
-
-
- void
- showExplainBox(void)
- /* Shows the dialog explaining the purpose of this program. */
- {
- Boolean gotEvent;
- DialogPtr explainDLOG;
- EventRecord event;
-
- /* Create the 'explain' dialog. */
- explainDLOG=GetNewDialog(kExplainDLOGid,NIL,(DialogPtr)-1);
- /* Show the 'explain' dialog. */
- DrawDialog(explainDLOG);
- do {
- } while (!(gotEvent=WaitNextEvent(mDownMask+keyDownMask,&event,10L,NIL)));
- /* Dispose of the 'explain' dialog. */
- DisposDialog(explainDLOG);
- }
-
-
- short
- NumToolboxTraps(void)
- /* Returns the number of Toolbox traps. */
- {
- short result;
-
- if (NGetTrapAddress(_InitGraf,ToolTrap)==NGetTrapAddress(0xAA6E,ToolTrap))
- result=0x200;
- else result=0x400;
- return(result);
- }
-
-
- TrapType
- GetTrapType(short theTrap)
- /* Returns the type of 'theTrap'. */
- {
- TrapType result;
-
- if ((theTrap&kTrapMask)>0) result=ToolTrap;
- else result=OSTrap;
- return(result);
- }
-
-
- Boolean
- TrapAvailable(short theTrap)
- /* Returns TRUE if 'theTrap' is available. */
- {
- Boolean result;
- TrapType theTrapType;
-
- theTrapType=GetTrapType(theTrap);
- if (theTrapType==ToolTrap) {
- theTrap=(theTrap&0x07FF);
- if (theTrap>=NumToolboxTraps()) theTrap=_Unimplemented;
- }
- result=(NGetTrapAddress(theTrap,theTrapType)!=NGetTrapAddress(_Unimplemented,ToolTrap));
- return(result);
- }
-
-
- void
- describeError(short id)
- /* Presents the user with an alert box describing the error that was */
- /* (hopefully) not my fault. */
- {
- Str255 str;
-
- switch(id) {
- /* Error handling for this program. */
- case kCantRun : GetIndString(str,kErrorSTRs,kNoAliasMgrStr);
- break;
- case kCantOpen : GetIndString(str,kErrorSTRs,kCantOpenStr);
- break;
- /* Default message. */
- default : GetIndString(str,kErrorSTRs,kDefaultStr);
- break;
- }
-
- /* Display the error message. */
- ParamText(str,"\p","\p","\p");
- Alert(kErrorALRTid,NIL);
-
- /* Set 'gQuitDemand' to TRUE. */
- gQuitDemand=TRUE;
- }